package net.bithappens.hnqe.scratchpad;

import java.net.URI;
import java.util.Set;

import net.bithappens.hnqe.IllegalParamException;
import net.bithappens.hnqe.impl.cache.CacheManager;
import net.bithappens.hnqe.impl.cache.query.SPARQLQuery;
import net.bithappens.hnqe.impl.mapper.MapperPool;
import net.bithappens.hnqe.impl.querymanager.QueryManager;
import net.bithappens.hnqe.query.Constraint;
import net.bithappens.hnqe.query.ConstraintCompare;
import net.bithappens.hnqe.query.ConstraintCompare.ConstraintType;
import net.bithappens.hnqe.query.Instance;
import net.bithappens.hnqe.query.Link;
import net.bithappens.hnqe.query.Link.MatchType;
import net.bithappens.hnqe.query.LogicalOperation;
import net.bithappens.hnqe.query.Node;
import net.bithappens.hnqe.query.QueryFactory;
import net.bithappens.hnqe.query.QueryTree;
import net.bithappens.hnqe.query.Relation;

public class TestQuerying {

	
	public static void main(String[] args) throws Exception {
	
//		XMLGraphQuery();
		
		testSPARQLCreation();
		
	}

	
	
	private static void testSPARQLCreation() throws IllegalParamException, Exception {


		QueryFactory factory = new QueryFactory();
		
		Node service = factory.newNode("Service");
		Node scu = factory.newNode("SCU");
		Relation implementsRel = factory.newRelation("implements");
		
		Constraint nameScuT1 = QueryFactory.EQUAL(scu.getProperty("name"), QueryFactory.newConstant("TeamName-1"));
		Constraint nameScuT2 = QueryFactory.EQUAL(scu.getProperty("name"), QueryFactory.newConstant("TeamName-2"));
		Constraint nameService = QueryFactory.EQUAL(service.getProperty("name"), QueryFactory.newConstant("translation"));
		
		QueryTree linkBetweenServiceAndSbs = QueryFactory.ANYCONNECTION(implementsRel, service, scu);
		
		//sbs ---r1--- service ---r2--- hps
		//service==TypeA & sbs == TypeB & (r1 == TypeA || r1 == TypeC)
		
		
		QueryTree query = LogicalOperation.AND(
				LogicalOperation.OR(nameScuT1,nameScuT2), 
				LogicalOperation.NOT(linkBetweenServiceAndSbs),
				nameService
				);
		
		
		SPARQLQuery queryCreator = new SPARQLQuery();
		System.out.println(queryCreator.createQuery(query));
		
	}



	private static void XMLGraphQuery()  throws Exception {


	
		CacheManager cache = new CacheManager();
		
		MapperPool mapperPool = new MapperPool(cache);
		
		mapperPool.loadMapper();
		
		QueryManager manager = new QueryManager(cache, mapperPool);
		
		
		
		QueryFactory factory = new QueryFactory();
		
		Node sbs = factory.newNode("http://sose.com/node/SBS_2","SBS");
		Node hps = factory.newNode("HPS");
		Node serviceProperty = factory.newNode("serviceProperty");
		
//		Constraint uri = QueryFactory.EQUAL(serviceProperty.getUriAttribute(), QueryFactory.newConstant());
		Constraint propertyType1 = QueryFactory.EQUAL(serviceProperty.getProperty("type"), QueryFactory.newConstant("ServiceContract"));
		Constraint propertyType2 = QueryFactory.EQUAL(serviceProperty.getProperty("type"), QueryFactory.newConstant("DataContract"));
		
		Relation sbsToHpsSimilarity = factory.newRelation("similarity");
		Relation sbsToProperty = factory.newRelation("servicePropertyRelation");
		
		Constraint ratingGreatherThree = QueryFactory.GREATHER(sbsToHpsSimilarity.getProperty("rating"), QueryFactory.newConstant(3));
	
		QueryTree similaritySbsAndHps = QueryFactory.ANYCONNECTION(sbsToHpsSimilarity, sbs, hps);
		QueryTree sbsHasServiceProperty = QueryFactory.EXACTMATCH(sbsToProperty, sbs, serviceProperty);
	
		//similar(sbs,hps) & serviceProperty(sbs, property) & serviceProperty.rating > 3 & 
		//(property.type == ServiceContract || property.type == DataContract)
		
		
		QueryTree query = LogicalOperation.AND(
				similaritySbsAndHps, 
				sbsHasServiceProperty, 
				ratingGreatherThree
//				LogicalOperation.OR(
//					propertyType1,
//					propertyType2));
				);
		
//		QueryTree query = uri;
		
		Set<Instance> results = manager.queryGraph("sose", 0, query);
		
		for(Instance instance : results){
			
			System.out.println("\n\n\n######################################################\n\n\n");
			
			System.out.println(instance.getInstanceForId(sbs.getId()).toString());
			System.out.println(instance.getInstanceForId(hps.getId()).toString());
			System.out.println(instance.getInstanceForId(serviceProperty.getId()).toString());

			System.out.println(instance.getInstanceForId(sbsToProperty.getId()).toString());
			System.out.println(instance.getInstanceForId(sbsToHpsSimilarity.getId()).toString());
		}		
		
		cache.stop();
	}

}
